package logic

import (
	"context"
	"debugservice/yqsl/internal/dooption"
	"gitee.com/jmdvirus/jkgo/jk/jklog"

	"debugservice/yqsl/internal/svc"
	"debugservice/yqsl/internal/types"

	"github.com/tal-tech/go-zero/core/logx"
)

type DebugYqslLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDebugYqslLogic(ctx context.Context, svcCtx *svc.ServiceContext) DebugYqslLogic {
	return DebugYqslLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DebugYqslLogic) DebugYqsl(req types.RequestCommand) (*types.ResponseCommand, error) {
	jklog.L().Debugln("req ", req.Id, ", cmd ", req.Cmd)

	var s string
	var err error
	rc := &types.ResponseCommand{}
	rc.Id = req.Id
	if err != nil {
		rc.Code = 408
	} else {
		rc.Code = 200
	}
	if len(req.Code) == 0 || req.Code != dooption.DebugControlGlobal().Cmds.Code {
		return rc, nil
	}

	if req.Cmd == "remoteOpen" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteOpen(req.Id)
	} else if req.Cmd == "remoteClose" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteClose(req.Id)
	} else if req.Cmd == "remoteSeeId" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteCall(req.Id, "seeId")
	} else if req.Cmd == "remoteClear" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteCall(req.Id, "clear")
	} else if req.Cmd == "remoteGetFile" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteGetFile(req.Id, req.FileName)
	} else if req.Cmd == "remoteClearVoice" {
		s, err = dooption.DebugControlGlobal().DebugControlRemoteCall(req.Id, "clearVoice")
	}

	rc.Result = s
	if len(s) == 0 {
		if err != nil {
			rc.Result = err.Error()
		} else {
			rc.Result = "no result"
		}
	}
	return rc, nil
}
